package com.example.walletdemo.controller;

import com.example.walletdemo.entity.Bank;
import com.example.walletdemo.entity.Order;
import com.example.walletdemo.entity.Record;
import com.example.walletdemo.entity.Wallet;
import com.example.walletdemo.entity.vo.BalanceVo;
import com.example.walletdemo.service.WalletService;
import com.example.walletdemo.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "钱包接口")
@RestController
@RequestMapping("/wallet")
public class WalletController {

    @Autowired
    private WalletService walletService;


    @ApiOperation(value = "根据用户名获取用户余额")
    @GetMapping("/balance")
    public ResultUtil getBalance(@RequestParam(value = "username") String username){

        if (StringUtils.isEmpty(username)){
            return ResultUtil.fail("用户名不能为空");
        }

        Wallet wallet = new Wallet();

        wallet = walletService.selectWalletByUserName(username);

        if (wallet!=null){
            return ResultUtil.success(wallet);
        }

        return ResultUtil.fail();

    }


    @ApiOperation(value = "消费")
    @PostMapping("/pay")
    public ResultUtil pay(@RequestParam(value = "commodity") String commodity,@RequestParam(value = "money") double money,
                          @RequestParam(value = "wid") int wid){

        Wallet wallet = new Wallet();

        wallet = walletService.selectWalletById(wid);

        if (wallet.getBalance()<money){
            return ResultUtil.fail("支付失败，钱包内余额不足");
        }

        Date date = new Date();


        Order order = new Order();
        order.setCommodity(commodity);
        order.setMoney(money);
        order.setWid(wid);
        order.setStatus("1");
        order.setCreateTime(date);
        order.setUpdateTime(date);

        boolean finish = walletService.insertOrder(order,wallet);

        if (finish){

            Record record = new Record();
            record.setMoney(order.getMoney());
            record.setCreateTime(date);
            record.setWid(order.getWid());
            record.setRid(order.getOid());
            record.setBalance(wallet.getBalance());
            record.setRType("1");

            walletService.insertRecord(record);


            BalanceVo balanceVo = new BalanceVo();

            balanceVo.setUsername(wallet.getUsername());
            balanceVo.setMoney(order.getMoney());
            balanceVo.setBalance(wallet.getBalance());
            balanceVo.setTime(date);

            return ResultUtil.success("消费成功",balanceVo);


        }

        return ResultUtil.fail();

    }




    @ApiOperation(value = "退款")
    @PostMapping("/refun")
    public ResultUtil refund(@RequestParam(value = "oid") int oid){

        Order orders = walletService.selectOrdersByOid(oid);

        orders.setUpdateTime(new Date());

        if (!orders.getStatus().equals("1")){
            return ResultUtil.fail("该商品无法退款");
        }

        boolean refund = walletService.refund(orders);

        if (refund){

            Wallet wallet =walletService.selectWalletById(orders.getWid());

           Record record = new Record();

           record.setRType("2");
           record.setWid(orders.getWid());
           record.setMoney(orders.getMoney());
           record.setCreateTime(orders.getUpdateTime());
           record.setBalance(wallet.getBalance());

           walletService.insertRecord(record);


           BalanceVo balanceVo = new BalanceVo();




           balanceVo.setBalance(wallet.getBalance());
           balanceVo.setTime(orders.getUpdateTime());
           balanceVo.setUsername(wallet.getUsername());
           balanceVo.setMoney(orders.getMoney());

           return ResultUtil.success("退款成功",balanceVo);
        }

        return ResultUtil.fail();


    }




    @ApiOperation(value = "获取消费记录")
    @GetMapping("/getRecord")
    public ResultUtil<List<Record>> getRecord(@RequestParam(value = "wid") int wid){

        List<Record> recordList = new ArrayList<>();

        recordList = walletService.selectRecordByWid(wid);


        return ResultUtil.success(recordList);


    }


    @ApiOperation(value = "提现")
    @PostMapping("/reflect")
    public ResultUtil reflect(@RequestParam(value = "cardNo") String cardNo,@RequestParam(value = "money") double money){


        Date date = new Date();

        Wallet wallet = new Wallet();

        wallet = walletService.selectWalletBycardNo(cardNo);

        wallet.setUpdateTime(date);

        if (wallet.getBalance() < money){

            return ResultUtil.fail("钱包内余额不足");

        }


        if(walletService.reflect(wallet,cardNo,money)){


            Record record = new Record();

            record.setCreateTime(date);
            record.setWid(wallet.getId());
            record.setMoney(money);
            record.setRType("4");
            record.setBalance(wallet.getBalance());

            walletService.insertRecord(record);


            BalanceVo balanceVo = new BalanceVo();

            balanceVo.setBalance(wallet.getBalance());
            balanceVo.setMoney(money);
            balanceVo.setUsername(wallet.getUsername());
            balanceVo.setTime(date);

            return ResultUtil.success("提现成功",balanceVo);


        }

                return ResultUtil.fail();


    }



    @ApiOperation(value = "重置")
    @PostMapping("/recharge")
    public ResultUtil recharge(@RequestParam(value = "cardNo") String cardNo,@RequestParam(value = "money") double money){

        Date date = new Date();

        Bank bank = new Bank();

        bank = walletService.selectBankByCardNo(cardNo);

        bank.setUpdateTime(date);

        if (bank.getBalance() < money){
            return ResultUtil.fail("卡里余额不足");
        }

        if (walletService.recharge(bank,money)){


            Wallet wallet = new Wallet();

            wallet=walletService.selectWalletById(bank.getWid());

            Record record = new Record();

            record.setRType("3");
            record.setWid(bank.getWid());
            record.setMoney(money);
            record.setCreateTime(date);
            record.setBalance(wallet.getBalance());


            walletService.insertRecord(record);






            BalanceVo balanceVo = new BalanceVo();
            balanceVo.setBalance(wallet.getBalance());
            balanceVo.setUsername(wallet.getUsername());
            balanceVo.setTime(date);
            balanceVo.setMoney(money);


            return ResultUtil.success("充值成功",balanceVo);



        }
        return ResultUtil.fail();

    }

}
